$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        table:null,
        currentKey:null,
        init:function () {
            var $this = this;
            this.table = $('#list');
            $('.xh-datagrid-toolbar').parseToolbar();
            xhExtra.disableButton($("#bt_list_del"),'eu');
            xhExtra.disableButton($("#bt_list_upload"),'eu');
            this.table.treegrid({
                url: baseUrl+'bpm/template/getTemp', 
                queryParams: {},
                animate: false,
                onBeforeSelect:function (row) {
                    if (!$this.currentKey) {
                        return true;
                    } else  if ($this.currentKey == row['keyId']) {
                        $this.currentKey = null;
                        $this.table.treegrid('unselect', row['keyId']);
                        $this.table.treegrid('collapse', row['keyId']);
                        xhExtra.disableButton($("#bt_list_del"),'eu');
                        xhExtra.disableButton($("#bt_list_upload"),'eu');
                        return false;
                    }
                },
                onSelect:function (row) {
                    $this.currentKey = row['keyId'];
                    $this.table.treegrid('expand', row['keyId']);
                    xhExtra.enableButton($("#bt_list_del"),'eu');
                    if (row['sTag'] == 'processPlan') {
                        xhExtra.enableButton($("#bt_list_upload"),'eu');
                    }
                },
                onDblClickRow:function (row) {
                    $this.funObj().openEditForm(row);
                }
            });

            this.listener();
        },
        listener:function () {
            var $this = this;

            $('#bt_list_add').click(function () {
                $this.funObj().openAddForm();
            });
            
            $("#bt_list_del").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().askDelQuestion();
            });

            $("#bt_list_upload").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().uploadNode();
            });
            //搜索
            $('#bt_list_search').click(function () {
                $this.funObj().searchAction();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                openAddForm:function () {
                    var selection = $this.table.treegrid('getSelections');
                    if (selection.length <= 0) { //新增模板
                        window.top.xhTop.showModal({
                            title:'新增模板',
                            content: '<iframe src="'+modelUrl+'template/tempForm"  style="width: 100%;;border-style: none;padding: 0px;height: 250px"  scrolling="yes"   ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    } else  {
                        var keyId = selection[0]['keyId'];
                        var bMultiLevel = selection[0]['bMultiLevel'];
                        var name  = selection[0]['name'];
                        var arr = keyId.split('_');

                        if (arr.length === 1) { //新增子节点
                            window.top.xhTop.showModal({
                                title:'新增节点',
                                content: '<iframe src="'+modelUrl+'template/nodeForm?iTPId='+arr[0]+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
                                button:'提交',
                                backClose: false
                            });
                        } else if (arr.length === 2) { //新增子节点的子节点
                            if (bMultiLevel == 0){
                                window.top.xhTop.showError('错误', '该类型模板最多不能超过两层');
                                return false;
                            }
                            var iTPId = selection[0]['iTPId'];
                            if (arr[0] === 'workOrder' || arr[0] === 'itemImpl') {
                                window.top.xhTop.showModal({
                                    title:'新增节点',
                                    content: '<iframe src="'+modelUrl+'template/nodeForm?iTPId='+iTPId+'&iParentId='+arr[1]+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
                                    button:'提交',
                                    backClose: false
                                });
                            }
                        } else  {
                            return false;
                        }
                    }

                },
                askDelQuestion:function () {
                    var selection = $this.table.treegrid('getSelections');
                    if(selection.length <= 0) { //新增
                        window.top.xhTop.showError('错误', '请先选择一个节点');
                        return false;
                    }
                    var keyId = selection[0]['keyId'];
                    var arr = keyId.split('_');
                    if (arr.length === 1) {
                        xhExtra.askChangeStatus('bpm/template/delTemp',{id:arr[0]},null,'确定要删除该模板嘛?',function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.table.treegrid('reload');
                        });
                    } else if (arr.length === 2) {
                        xhExtra.askChangeStatus('bpm/template/delNode',{id:arr[1]},null,'确定要删除该节点嘛?',function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.table.treegrid('reload');
                        });
                    }
                },
                openEditForm:function (row) {
                    var keyId = row['keyId'];
                    var arr = keyId.split('_');
                    if (arr.length === 1) { //新增子节点
                        window.top.xhTop.showModal({
                            title:'编辑模板',
                            content: '<iframe src="'+modelUrl+'template/tempForm?iTPId='+arr[0]+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    } else if (arr.length === 2) {
                        var iTPId = row['iTPId'];
                        window.top.xhTop.showModal({
                            title:'编辑节点',
                            content: '<iframe src="'+modelUrl+'template/nodeForm?iTPId='+iTPId+'&iNodeId='+arr[1]+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    } else  {
                        return false;
                    }
                },
                uploadNode:function () {
                    var selection = $this.table.treegrid('getSelections');
                    if(selection.length <= 0) {
                        window.top.xhTop.showError('错误', '请先选择');
                        return false;
                    }
                    var keyId = selection[0]['keyId'];
                    var arr = keyId.split('_');
                    if (arr.length === 1) {
                        if(selection.length <= 0) {
                            window.top.xhTop.showError('错误', '请先选择一个子节点更新');
                            return false;
                        }
                    } else if (arr.length === 2) {
                        window.top.swal({
                            title:  '确认要更新该子节点嘛？',
                            showCancelButton: true,
                            cancelButtonText: '取消',
                            confirmButtonText: '确认',
                            allowOutsideClick: true,
                            showCloseButton: true,
                            showLoaderOnConfirm: true,
                            type: 'warning',
                            preConfirm: function() {
                                return new Promise(function (resolve, reject) {
                                    $.ajax({
                                        url:modelUrl+'plan/uploadContent',
                                        data:{iNodeId:arr[1]},
                                        type:'POST',
                                        dataType:'json',
                                        success:function (data) {
                                            if(data['message']=='success'){
                                                window.top.swal('成功', '成功更新', 'success');
                                            }else {
                                                window.top.swal('错误', data['message'], 'error');
                                            }
                                        },
                                        error:function (data) {
                                            console.log(data);
                                        }
                                    });
                                })
                            }
                        }).then(function(isConfirm) {}, function(dismiss) {});

                    }
                },
                searchAction:function(){
                    $this.table.treegrid('reload',{
                        key : xhExtra.getValue($('#list_search'))
                    });
                }
            }
        }
    }
};